/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejercicio.practica9;

import java.util.LinkedList;
import java.util.Random;

/**
 *
 * @author manolo
 */
public class Matriz {

    private LinkedList buffer = new LinkedList();
    private boolean bufferLleno = false;
    private boolean bufferVacio = false;
    private LinkedList buffer2 = new LinkedList();
    private boolean buffer2Lleno = false;
    private boolean buffer2Vacio;
    Random rm = new Random();
    private final int N = 10;

    public Matriz() {
    }

    public void setBufferLleno(boolean bufferLleno) {
        this.bufferLleno = bufferLleno;
    }

    public void setBufferVacio(boolean bufferVacio) {
        this.bufferVacio = bufferVacio;
    }

    public void setBuffer2Lleno(boolean buffer2Lleno) {
        this.buffer2Lleno = buffer2Lleno;
    }

    public void setBuffer2Vacio(boolean buffer2Vacio) {
        this.buffer2Vacio = buffer2Vacio;
    }

    public synchronized void insertarDatos() {
          while (bufferLleno) { //control del limite maximo
            try {
                wait();
            } catch (InterruptedException ex) {
                System.out.println("Error: " + ex);
            }
        }
        int[][] m = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                m[i][j] = rm.nextInt(5);
            }
        }
        buffer.add(m);
        if (buffer.size() == 100) {
            bufferLleno = true;
            notifyAll(); //notificamos que se agregaron datos
        }
    }

    public synchronized void transpuestaMatriz(int n) {
        int[][] transpuesta = new int[N][N];
        int[][] m = null;
        while (buffer2Lleno) {
            try {
                wait(); //esperamos entonces
            } catch (InterruptedException ex) {
                System.out.println("Error: " + ex);
            }
        }
        m = (int[][]) buffer.poll();
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                transpuesta[i][j] = m[j][i];
            }
        }
        buffer2.add(transpuesta);
        if (buffer2.size() == 50) {
            buffer2Lleno = true;
            notify(); //notificamos que se agregaron datos
        }
        if (buffer.isEmpty()) {
            System.exit(0);
        }
        //bufferVacio = true;
    }

    public synchronized void diagonal(int n) {
          while (!buffer2Lleno) {
            try {
                wait();
            } catch (InterruptedException ex) {
                System.out.println("Error: " + ex);
            }
        }
        int res = 1;
        int[][] m = (int[][]) buffer2.poll();
        for (int i = 0; i < N; i++) {
            res *= m[i][i];
        }
        System.out.println("Diagonal matriz " + (n + 1) + " " + res);
        if (buffer2.isEmpty()) {
            buffer2Lleno = false;
            notifyAll();
        }
    }
}
